import {BmlLushu} from "vue-baidu-map";
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">

      <el-form-item label="站点+小组" prop="name" label-width="80px">
        <el-cascader
          v-model="cascaderId"
          :options="cascaderList"
          @change="cascaderChange"
          :props="props"
        :clearable="true"></el-cascader>
      </el-form-item>

<!--      <el-form-item label="站点名称" prop="siteId">-->
<!--        <el-select v-model="queryParams.siteId" placeholder="请选择站点" clearable size="small">-->
<!--          <el-option v-for="(item,index) in siteList" :key="index" :value="item.id" :label="item.name"/>-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="参赛人" prop="certName">
        <el-input
          v-model="queryParams.certName"
          placeholder="请输入参赛人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

<!--      <el-form-item label="分组" prop="groupId">-->
<!--        <el-select v-model="queryParams.groupId" placeholder="请选择类型" clearable size="small">-->
<!--          <el-option-->
<!--            v-for="item in groupList"-->
<!--            :key="item.id"-->
<!--            :label="item.name"-->
<!--            :value="item.id"-->
<!--          ></el-option>-->
<!--        </el-select>-->
<!--      </el-form-item>-->

      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
<!--          <el-option label="未提交" value="0" />-->
          <el-option label="审核中" value="1" />
          <el-option label="报名成功" value="2" />
          <el-option label="审核未通过" value="3" />
          <el-option label="已退赛" value="4" />
          <el-option label="退赛待审核" value="5" />
          <el-option label="退赛未通过" value="6" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['hps:enroll:exportSiteEnroll']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-success"
          size="mini"
          @click="handleCheck"
          v-hasPermi="['hps:enroll:checkapplybatch']"
        >报名通过</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-success"
          size="mini"
          @click="handleCheck2"
          v-hasPermi="['hps:enroll:checkoutbatch']"
        >退赛通过</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          :disabled="multiple"
          icon="el-icon-success"
          size="mini"
          @click="handleAuditBack"
          v-hasPermi="['hps:enroll:rollBackAudit']"
        >退回待审核</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          :disabled="multiple"
          icon="el-icon-success"
          size="mini"
          @click="handleCheckOut"
          v-hasPermi="['hps:enroll:checkapply']"
        >报名不通过</el-button>
      </el-col>

<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['hps:enroll:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->

    </el-row>

    <el-table v-loading="loading" :data="enrollList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="id" align="center" prop="id" />-->
      <el-table-column label="站点名称" align="center" prop="name" />
      <el-table-column label="分组" align="center" prop="groupName" width="150"/>
      <el-table-column label="参赛人" align="center" prop="certName" width="100">
        <template slot-scope="scope">
          <el-button type="text" @click="openAthletes(scope.row.id)">{{scope.row.certName}}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="缴费编号" align="center" prop="orderNo" />
      <el-table-column label="缴费金额" align="center" prop="totalAmount" width="80"/>
      <el-table-column label="缴费时间" align="center" prop="payTime" width="160"/>
      <el-table-column label="退赛原因" align="center" prop="outCause" show-overflow-tooltip/>
      <el-table-column label="退赛时间" align="center" prop="outTime" width="160"/>
      <el-table-column label="状态" align="center" prop="status" width="160">
        <template slot-scope="scope">
<!--          <div v-if="scope.row.status==0">未提交</div>-->
          <div v-if="scope.row.status==1">待审核</div>
          <div v-if="scope.row.status==2">报名成功</div>
          <div v-if="scope.row.status==3">审核未通过</div>
          <div v-if="scope.row.status==4">已退赛</div>
          <div v-if="scope.row.status==5">退赛待审核</div>
          <div v-if="scope.row.status==6">退赛未通过</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-error"
            @click="handleCheckOut(scope.row)"
            v-hasPermi="['hps:enroll:checkapply']"
            v-if="scope.row.status==1"
          >审核不通过</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-error"
            @click="handleCheckOut2(scope.row)"
            v-hasPermi="['hps:enroll:checkout']"
            v-if="scope.row.status==5"
          >审核不通过</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-error"
            style="color: #ff3f3f"
            @click="handleCheckQuery(scope.row)"
            v-if="scope.row.status==3"
          >未通过原因</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['hps:enroll:remove']"-->
<!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <el-dialog class="abow_dialog" title="运动员信息" :visible.sync="dialogFormVisible" @close="close">
      <div>
        <div class="top">
          <div class="size">姓名：{{traiInfo.certName}}</div> <div class="size">性别：<span v-if="traiInfo.sex==1">男</span><span v-if="traiInfo.sex==2">女</span></div>
        </div>

        <div class="top">
          <div class="size">身份证号：{{traiInfo.certNo}}</div> <div class="size">联系电话：{{traiInfo.phone}}</div>
        </div>

        <div class="top">
          <div class="size">紧急联系人：{{traiInfo.emergencyContact}}</div> <div class="size">车队/俱乐部： <el-input v-model="updateForm.club" style="width: 70%;" /> </div>
        </div>

        <div class="top">
          <div class="size">紧急联系人电话：{{traiInfo.emergencyPhone}}</div>
        </div>

        <div class="top">
          <div class="size">备注信息：{{traiInfo.remark}}</div>
        </div>

        <div class="top">
          <div class="size">附件信息：
            <el-image v-for="url in urls" :key="url" :src="url" style="width: 100px; height: 100px;margin-left: 20px"
                      :preview-src-list="urls" z-index="20000"></el-image></div>
        </div>
      </div>


      <div class="size" style="margin-top: 30px">骑行轨迹：</div>
      <TrailMapInfo v-if="dialogFormVisible" :lat="traiForm.lat" :lng="traiForm.lng" :value="traiForm.traiId"/>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitInfo">保存</el-button>
        <el-button @click="close">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 审核不通过，审核意见 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" >

        <el-form-item label="报名审核意见" prop="applyAudit" v-if="check==1">
          <el-input
            type="textarea"
            :rows="4"
            placeholder="报名审核意见"
            v-model="form.applyAudit">
          </el-input>
        </el-form-item>

        <el-form-item label="退赛审核意见" prop="outAudit" v-if="check==2">
          <el-input
            type="textarea"
            :rows="4"
            placeholder="退赛审核意见"
            v-model="form.outAudit">
          </el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :loading="readLoad">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="visible1" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" >
        <el-form-item label="原因" prop="applyAudit">
          <el-input
            type="textarea"
            :rows="4"
            placeholder="审核意见"
            v-model="form.applyAudit"
            readonly>
          </el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="visible1 = false">关 闭</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import {
  getEnroll,
  delEnroll,
  addEnroll,
  updateEnroll,
  exportEnroll,
  listEnroll,
  checkApply,
  checkOut,
  checkApplyBatch,
  checkOutBatch,
  exportSiteEnroll,
  rollBackAudit
} from "@/api/hps/enroll";
import {listGroup} from '@/api/hps/group'
import TrailMapInfo from '@/views/hps/trail/trailMapInfo';
import {cascaderSite, listSite} from "@/api/hps/site";

export default {
  name: "siteEnroll",
  components: {
    TrailMapInfo
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      readLoad: false,
      // 总条数
      total: 0,
      //判断报名或退赛、1报名、2退赛
      check:0,
      // 报名信息表格数据
      enrollList: [],
      //运动员详情附件
      urls:[],
      baseUrl: process.env.VUE_APP_BASE_API,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      visible1: false,
      dialogFormVisible: false,
      //报名详细信息
      traiInfo:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        certName: undefined,
        name: undefined,
        status: undefined,
        groupName:undefined,
        dataType:'0',
        siteId: undefined,
        groupId: undefined
      },
      cascaderId: undefined,
      cascaderList: [],
      props: {
        value: "id",
        label: "name",
        children: "hpsSiteGroupList",
        checkStrictly: true
      },
      // 表单参数
      form: {
        id:undefined,
        applyAudit:undefined,
        outAudit:undefined,
        status:undefined
      },
      traiForm:{
        traiId:undefined,
        lng:undefined,
        lat:undefined
      },
      updateForm:{
        id:undefined,
        club: undefined
      },
      // 表单校验
      rules: {
        outAudit: [
          {required: true, message: "退赛审核意见不能为空", trigger: "blur"}
        ],
      },
      groupList:[],
      siteList: []
    };
  },
  created() {
    cascaderSite().then(response => {
      this.cascaderList = response.data
      this.cascaderList.forEach(item => {
        item.id = item.id
        if (item.hpsSiteGroupList && item.hpsSiteGroupList.length > 0) {
          item.hpsSiteGroupList.forEach(i => {
            i.id = i.groupId;
            i.name = i.groupName
          })
        } else {
          item.hpsSiteGroupList = undefined
        }
      })
    })
    this.getList();
    // this.getGroupList();

    // listSite({
    //   dataType: '0'
    // }).then(res=>{
    //   this.siteList = res.rows;
    // })
  },
  methods: {
    /** 查询报名信息列表 */
    getList() {
      this.loading = true;
      listEnroll(this.queryParams).then(response => {
        this.enrollList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    getGroupList(){
      listGroup().then(res=>{
        this.groupList = res.rows;
      })
    },

    //打开运动员信息
    openAthletes(id){
      getEnroll(id).then(response => {
        this.traiInfo=response.data;
        this.traiForm.lat=response.data.latitude;
        this.traiForm.lng=response.data.longitude;
        this.traiForm.traiId=response.data.id;
        this.updateForm.id = response.data.id;
        this.updateForm.club = response.data.club;

        //把附件地址转成数组
        var arry=[];
        if(response.data.pics==null){
          arry=[''];
        }else{
          arry =response.data.pics.split(',');
        }
        //清理数组最后一个空格
        var newArry=[];
        var baseUrl=this.baseUrl;
        arry.map(function(item){
          if(item!='' && item!=undefined)
          {
            newArry.push(baseUrl + item)
          }
        })
        this.urls=newArry;
        console.log(this.urls)
        this.dialogFormVisible=true;
      });

    },

    //关闭运动员信息
    close(){
      this.traiForm.lat=undefined;
      this.traiForm.lng=undefined;
      this.traiForm.traiId=undefined;
      this.urls=[];
      this.dialogFormVisible=false;
      this.updateForm.id = undefined;
      this.updateForm.club = undefined;
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id:undefined,
        applyAudit:undefined,
        outAudit:undefined,
        status:undefined
      };
      this.check = 0;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.cascaderId = undefined;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加报名信息";
    },
    /** 报名审核不通过 */
    handleCheckOut(row) {
      this.reset();
      let id = row.id || this.ids

      this.check=1;
      this.form.id = id;
      this.open = true;
      this.title = "审核意见";
    },
    // 报名成功退回待审核状态
    handleAuditBack(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认退回待审核状态，报名信息编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return rollBackAudit(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("退回成功");
      }).catch(function() {});
    },

    /** 退赛审核不通过 */
    handleCheckOut2(row) {
      this.reset();
      const id = row.id || this.ids
      this.check=2;
      this.form.id = id;
      this.open = true;
      this.title = "审核意见";
      // this.check=0;
    },
    // 查看审核不通过原因
    handleCheckQuery(row) {
      this.reset();
      const id = row.id || this.ids
      getEnroll(id).then(response => {
        this.title = "未通过原因";
        this.form.applyAudit = row.applyAudit
        this.visible1 = true
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            if(this.check==1){

              let query = ""
              if (this.ids.length > 0) {
                for (let i = 0; i < this.ids.length;i++) {
                  this.ids[i] = "ids=" + this.ids[i]
                }
                query = this.ids.join("&")
              } else {
                query = "ids=" + this.form.id;
              }
              if (this.form.applyAudit != undefined && this.form.applyAudit != "") {
                query = query + "&applyAudit=" + this.form.applyAudit;
              }
              this.syncData(query);
            }

            if(this.check==2){

              checkOut(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("操作成功");
                  this.open = false;
                  this.getList();
                }
              });
            }

          }
        }
      });
    },
    syncData(query){
      var that = this;
      this.readLoad = true;
      checkApply(query).then(response => {
        that.getList();
        that.readLoad = false;
        that.$alert(response.msg, "生成结果", { dangerouslyUseHTMLString: true,type: "success" });
        that.open = false;
      }).catch(function(res) {
        that.getList();
        that.readLoad = false;
        that.open = false;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除报名信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delEnroll(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },

    /** 报名审核按钮操作 */
    handleCheck(row) {
      const ids = row.id || this.ids;
      console.log(ids)
      if(ids==undefined || ids==null ||ids.length==0){
        this.$message.error('请勾选至少一个待审核报名');
      }else{
        this.$confirm('是否确认审核通过报名信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return checkApplyBatch(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("操作成功");
        }).catch(function() {});
      }

    },
    /** 退赛审核按钮操作 */
    handleCheck2(row) {
      const ids = row.id || this.ids;
      if(ids==undefined || ids==null ||ids.length==0){
        this.$message.error('请勾选至少一个待退赛报名');
      }else{
        this.$confirm('是否确认审核通过报名信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return checkOutBatch(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("操作成功");
        }).catch(function() {});
      }

    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有参赛信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportEnroll(queryParams);
        }).then(response => {
          this.download(response.msg,"环鄱赛参赛名单.xlsx");
        }).catch(function() {});
    },

    submitInfo(){
      if (this.updateForm.club) {
        updateEnroll(this.updateForm).then(response => {
          if (response.code === 200) {
            this.msgSuccess("操作成功");
            this.getList();
            this.close();
          }else {
            this.msgError("操作失败");
            this.close();
          }
        });
      }
    },
    cascaderChange(value) {
      // console.log(value);
      this.cascaderId = value;
      if (value == undefined || value.length == 0) {
        this.queryParams.siteId = undefined;
        this.queryParams.groupId = undefined;
      } else {
        if (value.length == 1) {
          this.queryParams.siteId = value[0]
        }
        if (value.length == 2) {
          this.queryParams.siteId = value[0]
          this.queryParams.groupId = value[1]
        }
      }
    }
  }
};
</script>

<style>
  .size{
    font-size: 18px;
    width: 50%;
  }
  .top{
    display: flex;
    margin-top: 30px;
  }
</style>
